﻿/*
 * 作者: 谢中涞,博客园:http://www.cnblogs.com/xie-zhonglai
 * 欢迎转载,但请保留出处
 * */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Xiezl.Common.EFExpressions;

namespace Xiezl.Common.Repositorys
{
    /// <summary>
    /// 封装查询参数
    /// </summary>
    /// <typeparam name="TExpression">参数类型</typeparam>
    public abstract class QueryParam<TExpression>
    {
        /// <summary>
        /// 查询条件
        /// </summary>
        public TExpression Expression { get; set; }

        /// <summary>
        /// 排序字段
        /// </summary>
        public string OrderPropertyName { get; set; }

        /// <summary>
        /// 是否是升序排序,为false时则对OrderPropertyName采取降序排序
        /// </summary>
        public bool IsAscSort { get; set; }

        /// <summary>
        /// 分页查询,查询第几页
        /// </summary>
        public int PageIndex { get; set; }

        /// <summary>
        /// 分页查询,每页显示记录数
        /// </summary>
        public int PageSize { get; set; }

        /// <summary>
        /// 指示是否是分页查询
        /// </summary>
        public bool IsPagingQuery { get { return PageSize > 0; } }

    }
    /// <summary>
    /// EF 实现的 QueryParam
    /// </summary>
    /// <typeparam name="TEntity">条件关联实体类型</typeparam>
    public class EFQueryParam<TEntity> : QueryParam<Expression<Func<TEntity, bool>>> where TEntity : class
    {
        /// <summary>
        /// 实例化新的 EFQueryParam 实例
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <param name="orderPropertyName">排序属性字段名</param>
        /// <param name="isAscSort">是否是升序排序,false为降序排序</param>
        /// <param name="pageIndex">分页查询,查询第几页</param>
        /// <param name="pageSize">分页查询,每页显示记录数</param>
        public EFQueryParam(Expression<Func<TEntity, bool>> exp, string orderPropertyName,
            bool isAscSort, int pageIndex, int pageSize)
        {
            this.Expression = exp;
            this.OrderPropertyName = orderPropertyName;
            this.IsAscSort = isAscSort;
            this.PageIndex = pageIndex;
            this.PageSize = pageSize;
        }

        /// <summary>
        /// 实例化新的 EFQueryParam 实例
        /// </summary>
        /// <param name="exp">查询条件</param>
        /// <param name="orderPropertyName">排序属性字段名</param>
        /// <param name="isAscSort">是否是升序排序,false为降序排序</param>
        /// <param name="pageIndex">分页查询,查询第几页</param>
        /// <param name="pageSize">分页查询,每页显示记录数</param>
        public EFQueryParam(IEFQueryExpression<TEntity> exp, string orderPropertyName,
            bool isAscSort, int pageIndex, int pageSize) :
            this(exp.GetExpression(), orderPropertyName, isAscSort, pageIndex, pageSize)
        {
        }
    }
}
